# -*- coding: utf-8 -*-
# @Time : 2024/3/1 21:41
# @Author : Hami
import yaml

# 用来读取和写入excel\yaml
from config import *
import openpyxl
from Common.encryptAes import ENCRYPTAES


class FileDataDriver:
    @staticmethod  # 直接通过类名调用，不要self
    def readExcel(file_path=CASEDATAURL, sheet_name=SHEETNAME):
        # 打开现有的Excel文件或创建新的文件
        try:
            workbook = openpyxl.load_workbook(file_path)
        except FileNotFoundError:
            workbook = openpyxl.Workbook()

        # 选择或创建指定的工作表
        if sheet_name in workbook.sheetnames:
            worksheet = workbook[sheet_name]
        else:
            worksheet = workbook.create_sheet(sheet_name)

        # 获取列名, 获取所有的key  ，并且遍历变成一个列表
        headers = [cell.value for cell in worksheet[2]]
        # print("所有的header（key）", headers)

        # 将数据存储到列表当中
        data = []
        # 把小的数据从第三行开始
        for row in worksheet.iter_rows(min_row=3, values_only=True):
            # 无论什么时候会直接加入到data当中。
            # data.append(dict(zip(headers, row)))

            # 判断是否是：是，就加
            current_data = dict(zip(headers, row))
            if current_data["is_true"] == True:
                data.append(current_data)
        workbook.close()
        return data

    @staticmethod
    def writeDataToExcel(file_path=CASEDATAURL, sheet_name=SHEETNAME, row=None, column=None, value=None):
        # 打开现有的Excel文件或创建新的文件
        try:
            workbook = openpyxl.load_workbook(file_path)
        except FileNotFoundError:
            workbook = openpyxl.Workbook()

        # 选择或创建指定的工作表
        if sheet_name in workbook.sheetnames:
            worksheet = workbook[sheet_name]
        else:
            worksheet = workbook.create_sheet(sheet_name)

        # 写入数据到指定行和列
        worksheet.cell(row=row, column=column).value = value

        # 保存修改后的文件 -- 文件不能是打开的状态，不然写入会失败
        workbook.save(file_path)

    @staticmethod
    def read_yaml(file_path=YAMLDATAURL):
        with open(file_path, "r", encoding="utf-8") as file:
            # 读取
            data = yaml.safe_load(file)
            return data

    @staticmethod
    def write_yaml(file_path=YAMLDATAURL, data=None):
        with open(file_path, "w", encoding="utf-8") as file:
        # 写入
        # allow_unicode = True，避免将中文字符转换为 Unicode 转义序列
            yaml.dump(data, file, allow_unicode=True)

    @staticmethod
    def data_encryptAes(data):
        """
        把对应的数据进行Aes加密(有可能需要加密，有可能不需要加密)
        @param data:
        @return:
        """
        # {"username":"admin","password":"123456"}
        # {"@username":"admin","@password":"123456"}
        # print("原始数据是：",data)
        newdata = {}
        for key in data:
            if key[0] == "@":
                # 需要加密
                # 把数据进行加密之后，要恢复原本的key（不要@符号--为了打个标识）
                #  字典名[key] = value
                newdata[key[1:]] = ENCRYPTAES.aes_encrypt(data[key])
            else:
                # 不需要加密
                newdata[key] = data[key]
        return newdata


# data = {"@username":"tony","@password":"123456"}
# data2 = FileDataDriver.data_encryptAes(data)
# print("加密后的数据是：", data2)

# print(FileDataDriver.readExcel())